<?php

namespace Modules\Api\Http\Controllers;

use HiCommon\Service\OrderPayService;
use HiCommon\Service\PayYanSongDaService;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Request;

class PayController extends BaseController
{

    /**
     * 线上支付
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function online_pay()
    {
        try {
            $n_id = intval(Request::input('id',0));//年检预约单id
            $n_uid = intval(api_user_id());//用户id
            $s_pay_type = str_trim(Request::input('pay_type',''));
            $s_order_type = str_trim(Request::input('order_type',''));

            //开启事务
            shiwu_start();
            $result = OrderPayService::api_online_pay($s_pay_type,$s_order_type,$n_id,$n_uid);
            shiwu_end($result);

            return  $result;
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 支付宝支付结果回调
     *
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function alipay_notify(){
        try {
            $arr_input = Request::input();

            //开启事务
            shiwu_start();
            $result = OrderPayService::api_notify_confirm_for_ali_pay($arr_input);
            shiwu_end($result);

            //通知支付宝服务器 确认支付成功
            if($result['state'] == 0){
                $pay = PayYanSongDaService::pay_ali();
                //            $data = $pay->verify(); // 是的，验签就这么简单！
                //            $json_data = $data->all();
                return $pay->success();
            }
            else{
                return  $result;
            }
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /*微信支付结果回调*/
    public function wxpay_notify(){
        try {
            $pay_wechat = PayYanSongDaService::pay_wechat();
            $data = $pay_wechat->verify(); // 是的，验签就这么简单！
            $json_data = $data->all();

            //开启事务
            shiwu_start();
            $result = OrderPayService::api_notify_confirm_for_wechat_pay($json_data);
            shiwu_end($result);

            //通知支付宝服务器 确认支付成功
            if($result['state'] == 0){
                return $pay_wechat->success();
//                return hello_success('确认成功');
            }
            else{
                return  $result;
            }

        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }

    /**
     * 订单支付结果查询
     *
     * @return array
     * @author wumengmeng <wu_mengmeng@foxmail.com>
     */
    public function order_result(){
        try {
            $n_id = intval(Request::input('id',0));//订单id
            $s_order_type = str_trim(Request::input('order_type',''));//订单类型

            $result = OrderPayService::api_order_result($n_id,$s_order_type);
            return  $result;
        }
        catch (\Exception $exception) {
            return hello_error('失败',[],$exception->getMessage());
        }
    }




}